import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import learning.xml.jaxb.MusicalStyle;

public class XML {
	public static String MUSICAL_STYLE;
	public static JAXBContext context; 
	public static MusicalStyle musicalStyle = new MusicalStyle();
	public static MusicalStyle musicalStyle2 = null;
//	public boolean learning=false;

	public void newStyle(String name) throws JAXBException, IOException {
		context = JAXBContext.newInstance(MusicalStyle.class);

		musicalStyle.setName(name);
		MUSICAL_STYLE=("./"+name+".xml");
	}
	
	public void setDuration(int[] duration){
		musicalStyle.setDuration(duration);
	}
	public void setCount(int count){
		musicalStyle.setCount(count);
	}
	public void setNotes(int[] notes){
		musicalStyle.setNotes(notes);
	}
	public void setVolume(int[] volume){
		musicalStyle.setVolume(volume);
	}
	
	public void writeStyle() throws JAXBException, IOException{
		// create JAXB context and instantiate marshaller
		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(musicalStyle, System.out);
		Writer w = null;
		try {
			w = new FileWriter(MUSICAL_STYLE);
			m.marshal(musicalStyle, w);
		} finally {
			try {
				w.close();
			} catch (Exception e) {
			}
		}
	}
	
	public boolean readStyle(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MUSICAL_STYLE=("./"+name+".xml");
		boolean exists=true;

		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			exists=false;
//			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			exists=false;
//			e.printStackTrace();
		}
		return exists;
	}
	
	
	
	public static boolean readStyleStatic(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MUSICAL_STYLE=("./"+name+".xml");
		boolean exists=true;

		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			exists=false;
//			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			exists=false;
//			e.printStackTrace();
		}
		return exists;
	}
	
	public static int[] getNotes(String name) throws JAXBException{
		// get variables from our xml file, created before
		System.out.println("name "+name );
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MusicalStyle musicalStyle2 = null;
		MUSICAL_STYLE=("./"+name+".xml");
		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[] notes = musicalStyle2.getNotes();
		return notes;
	}
	public static int[] getVolume(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MusicalStyle musicalStyle2 = null;
		MUSICAL_STYLE=("./"+name+".xml");
		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[] volume = musicalStyle2.getVolume();
		return volume;
	}
	
	
	public static int[] getDuration(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MusicalStyle musicalStyle2 = null;
		MUSICAL_STYLE=("./"+name+".xml");
		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[] duration = musicalStyle2.getDuration();
		return duration;
	}
	
	
	public static int getCount(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MusicalStyle musicalStyle2 = null;
		MUSICAL_STYLE=("./"+name+".xml");
		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int count = musicalStyle2.getCount();
		return count;
	}
	public void init() throws JAXBException, IOException {
		newStyle("default");
	}
}

//	how to use:
//		XML xml = new XML();
//		xml.newStyle("john_coltrane");
//		xml.writeStyle();
//		XML.readStyle("john_coltrane");